# coding:utf8
import sys
sys.path.extend(['C:\\Py', 'C:\\Py\\e_mail'])

from os import system, mkdir, popen, remove,getpid
from os.path import exists
import time, codecs
from e_mail.send.cl_send_mail import SendMail


print(f"进程已经创建{getpid()}")
nowTime = time.strftime('%Y%m%D%H')

SOURCE_DIR = [f'F:\\H_temp\\{time.strftime("%Y%m%d")}\\']
# SOURCE_DIR = ['F:\\迅雷下载', 'D:\\迅雷下载', 'F:\\H_zip\\Diszip']  # r'F:\H_zip\DisZip'
TARGET_DIR = f'F:\\H_temp\\{time.strftime("%Y%m%d")}\\'
EXE_UNZIP = "C:\\Program Files\\WinRAR\\WinRAR.exe"
EXE_NOTEPAD = r"C:\Program Files\Notepad++\notepad++.exe"
f_log = r'D:/SelfLog/unzip_ErrorLog.txt'
dic_sender = {'pop_host': 'pop.qq.com',
              'smtp_host': 'smtp.qq.com',
              'user': '1491311784@qq.com',
              'passwd': 'fxvvlznnwkclheic',
              }

print("initing ...")
if not exists('./_sup/'): mkdir('./_sup/')
if not exists('D:/SelfLog/'): mkdir('D:/SelfLog/')
if exists(f_log): remove(f_log)
if exists('./_sup/fileList.txt'): remove('./_sup/fileList.txt')
print(f"正在拉取{SOURCE_DIR}的文件列表")
for _dir in SOURCE_DIR: system(f'dir /b /a:-D /s  {_dir}  >> ./_sup/fileList.txt')  # D:

# print(open('./_sup/fileList.txt', 'r', encoding='gbk').read().split('\n'))
i, unzip_list = 1, ''
for f in open('./_sup/fileList.txt', 'r', encoding='gbk').read().split('\n'):
    unzip_list += f'{f}\n'
    if f.endswith('zip'):
        ss = f'"{EXE_UNZIP}" x  -o- -ilog{f_log} -ibck -inul -kb "{f}"  *.* "{TARGET_DIR}\\"'  # -ibck   -o-   -or
        print(ss)
        popen(ss).read()

try:
    with open(f_log, 'r', encoding='utf_16')as f:
        SendMail(**dic_sender).send_one_mail('lee_cq@icloud.com', '解压情况', f.read())
except: SendMail(**dic_sender).send_one_mail('lee_cq@icloud.com', '解压情况',
                                             f"[{time.strftime('%Y-%m-%D %H:%M')}]一切正常。"
                                             f"已经将所有文件提取到{TARGET_DIR}")
